package com.egan.pet.admin.controller;

import com.egan.entity.dto.ItemPageQueryDTO;
import com.egan.entity.dto.ProductDTO;
import com.egan.entity.po.Item;
import com.egan.entity.vo.ProductVO;
import com.egan.pet.admin.result.PageResult;
import com.egan.pet.admin.result.Result;
import com.egan.pet.admin.service.IItemService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author egan
 * @since 2024-09-09
 */
@RestController
@RequestMapping("/shopping")
@Tag(name = "商品相关接口")
public class ProductController {

    @Resource
    private IItemService itemService;

    @GetMapping("/product/{id}")
    @Operation(summary = "根据ID查询商品信息", description = "根据商品id查询商品信息")
    public Result<ProductVO> product(@PathVariable long id) {
        ProductVO productVO = itemService.getByIdWithDesc(id);
        return Result.success(productVO);
    }

    @PostMapping("/product")
    @Operation(summary = "新增商品信息", description = "新增一个商品，ID名称分类id不能为空")
    public Result save(@RequestBody ProductDTO productDTO) {
        itemService.saveItem(productDTO);
        return Result.success(productDTO);
    }

    @PostMapping("/page")
    @Operation(summary = "分页查询商品", description = "根据页数以及判断状态显示商品")
    public Result<PageResult> page(@RequestBody ItemPageQueryDTO itemPageQueryDTO) {
        PageResult pageResult = itemService.pageQuery(itemPageQueryDTO);
        return Result.success(pageResult);
    }

    @DeleteMapping
    @Operation(summary = "批量删除", description = "根据id批量删除物品")
    public Result<String> delete(@RequestParam List<Long> ids) {
        itemService.removeByIds(ids);
        return Result.success("删除成功" + ids);
    }

    @PutMapping
    @Operation(summary = "修改物品", description = "根据ID修改物品信息")
    public Result update(@RequestBody ProductDTO productDTO) {
        itemService.updateId(productDTO);
        return Result.success(productDTO);
    }

    @GetMapping("/list")
    @Operation(summary = "根据分类ID查找", description = "根据分类ID查找")
    public Result<List<Item>> queryCategoryById(Long cid) {
        List<Item> list = itemService.queryCategoryId(cid);
        return Result.success(list);
    }

    @PostMapping("/status/{status}")
    @Operation(summary = "起售或停售",description = "启动出售或者关闭出售功能")
    public Result startOrStop(@PathVariable Integer status,Long id){
        itemService.startOrStop(status,id);
        return Result.success();
    }
}
